package com.ruoyi.common.utils;
import org.springframework.http.*;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;

public class OpenRouterApiClient {

    private static final String PROVISIONING_API_KEY = "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";
    private static final String BASE_URL = "https://openrouter.ai/api/v1/keys";

    private static RestTemplate restTemplate = new RestTemplate();

    public OpenRouterApiClient() {
        this.restTemplate = new RestTemplate();
    }

    private static HttpHeaders createHeaders(String configKey) {
        HttpHeaders headers = new HttpHeaders();
        headers.set("Authorization", "Bearer " + configKey);
        headers.setContentType(MediaType.APPLICATION_JSON);
        return headers;
    }

    // List the most recent 100 API keys
    public static ResponseEntity<String> listRecentKeys(String configKey) {
        HttpEntity<String> entity = new HttpEntity<>(createHeaders(configKey));
        return restTemplate.exchange(BASE_URL, HttpMethod.GET, entity, String.class);
    }

    // Paginate using the offset parameter
    public static ResponseEntity<String> listKeysWithOffset(int offset,String configKey) {
        HttpEntity<String> entity = new HttpEntity<>(createHeaders(configKey));
        return restTemplate.exchange(BASE_URL + "?offset=" + offset, HttpMethod.GET, entity, String.class);
    }

    // Create a new API key
// Create a new API key
    public static ResponseEntity<String> createApiKey(String name, String configKey, Integer limit) {
        String requestBody = String.format("{\"name\": \"%s\", \"label\": \"%s\", \"limit\": %d}", name, name, limit);
        HttpEntity<String> entity = new HttpEntity<>(requestBody, createHeaders(configKey));
        try {
            System.out.println("Request Body: " + requestBody);
            String url = BASE_URL;
            System.out.println("Request URL: " + url);

            return restTemplate.exchange(url, HttpMethod.POST, entity, String.class);
        } catch (HttpClientErrorException e) {
            System.err.println("Error Response: " + e.getResponseBodyAsString());
            throw e;
        }
    }

    public static ResponseEntity<String> createApiKey(String name, String configKey) {
        return createApiKey(name,configKey,1);
    }

    // Get a specific key
    public static ResponseEntity<String> getApiKey(String keyHash,String configKey) {
        HttpEntity<String> entity = new HttpEntity<>(createHeaders(configKey));
        return restTemplate.exchange(BASE_URL + "/" + keyHash, HttpMethod.GET, entity, String.class);
    }

    // Update a key
    public static ResponseEntity<String> updateApiKey(String keyHash, String name, Boolean disabled,String configKey) {
        String requestBody = String.format("{\"name\": \"%s\", \"disabled\": %b}", name, disabled);
        HttpEntity<String> entity = new HttpEntity<>(requestBody, createHeaders(configKey));
        return restTemplate.exchange(BASE_URL + "/" + keyHash, HttpMethod.PUT, entity, String.class);
    }

    // Delete a key
    public static ResponseEntity<Void> deleteApiKey(String keyHash,String configKey) {
        HttpEntity<String> entity = new HttpEntity<>(createHeaders(configKey));
        return restTemplate.exchange(BASE_URL + "/" + keyHash, HttpMethod.DELETE, entity, Void.class);
    }

    public static void main(String[] args) {
        OpenRouterApiClient client = new OpenRouterApiClient();

        // Example usage
//        ResponseEntity<String> recentKeys = client.listRecentKeys();
//        System.out.println("Recent Keys: " + recentKeys.getBody());
//
//
        ResponseEntity<String> createdKey = createApiKey("Customer Instance Key", PROVISIONING_API_KEY, 1000);
        System.out.println("Created Key: " + createdKey.getBody());
//
//        ResponseEntity<String> paginatedKeys = client.listKeysWithOffset(100);
//        System.out.println("Paginated Keys: " + paginatedKeys.getBody());

        String keyHash = "3280980126c5ca44c4938b5e5a30ef27db65cd50041cd2060b1c561b441f0e15";

//
//        ResponseEntity<String> updatedKey = client.updateApiKey(keyHash, "123", true);
//        System.out.println("Updated Key: " + updatedKey.getBody());
//
//        ResponseEntity<Void> deletedKey = client.deleteApiKey(keyHash);
//        System.out.println("Deleted Key: " + deletedKey.getStatusCode());
//
//        ResponseEntity<String> specificKey = client.getApiKey(keyHash);
//        System.out.println("Specific Key: " + specificKey.getStatusCode());
    }
}
